import { toast } from "./utils.js"
export default function(){
	return new Promise((resolve,reject)=>{
		uni.showLoading({
			title:"登录中……"
		});
		
		uni.login({
			provider: 'weixin',
			success(e) {
				if (e.code) {
					uniCloud.callFunction({ 
						name: 'login',
						data: {
							code:e.code
						}
					}).then(res=>{
						uni.hideLoading()
						// console.log(res);
						if (res.result.status !== 0) {
							return Promise.reject(new Error(res.result.msg))
						}
						// 登录成功 返回 token,openid
						resolve(res)
					}).catch(err=>{
						uni.hideLoading()
						toast({
							title: err.message,
							icon: "none"
						})
					})
				} else {//获取登录code失败
					uni.hideLoading()
					toast({
						title: "微信登录失败",
						icon: "none"
					})
				}
			},//获取登录code失败
			fail(e) {
				uni.hideLoading()
				toast({
					title: "微信登录失败",
					icon: "none"
				})
			}
		})
	})
};
